﻿@page "/Fin/Receivable/Approve"
@using nERP.ViewModel.Fin.ReceivableVMs
@using nERP.Model.Fin
@using System.ComponentModel.DataAnnotations
@inherits BasePage

<div>
    <ValidateForm @ref="vform" Model="@Model" OnValidSubmit="@Submit">
        <Row ItemsPerRow="ItemsPerRow.Three" RowType="RowType.Normal">
            <Display @bind-Value="@Model.Entity.ReceivableCode" ShowLabel="true" />
            <Display @bind-Value="@Model.Entity.CustomerId" ShowLabel="true" Lookup="AllCustomers" />
            <Display @bind-Value="@Model.Entity.MakeUserId" ShowLabel="true" Lookup="AllMakeUsers" />
            <Display @bind-Value="@Model.Entity.ReceivableDate" ShowLabel="true" FormatString="yyyy-MM-dd" />
            <Display @bind-Value="@Model.Entity.SettlementAccountId" ShowLabel="true" Lookup="AllSettlementAccounts" />
            <Display @bind-Value="@Model.Entity.SettlementTypeId" ShowLabel="true" Lookup="AllSettlementTypes" />
            <Display @bind-Value="@Model.Entity.Status" ShowLabel="true" />
            <Display @bind-Value="@Model.Entity.TotalPrice" ShowLabel="true" FormatString="0.##" />
            <Display @bind-Value="@Model.Entity.Remark" ShowLabel="true" />
            <Display @bind-Value="@Model.Entity.CreateTime" ShowLabel="true" FormatString="yyyy-MM-dd HH:mm:ss" />
            <Display @bind-Value="@Model.Entity.UpdateTime" ShowLabel="true" FormatString="yyyy-MM-dd HH:mm:ss" />
            <Display @bind-Value="@Model.Entity.CreateBy" ShowLabel="true" />
            <Display @bind-Value="@Model.Entity.UpdateBy" ShowLabel="true" />
            <WTUploadImage @bind-Value="@Model.Entity.InvoicesImage" IsDisabled="true" thumb-width="128" />
        </Row>
        <Divider Icon="fa-fw fa-solid fa-bookmark" Text="@WtmBlazor.Localizer["_Model._Receivable._ReceivableDetail_Receivable"]" Alignment="Alignment.Left" />


        <Table TItem="ReceivableDetail"
               IsPagination="false"
               IsStriped="true" IsBordered="true" IsMultipleSelect="false" @bind-Items="@ReceivableDetailReceivableList2" ShowFooter="true"
               ShowToolbar="false" ShowExtendButtons="false" ShowSkeleton="true">
            <TableColumns>
                <TableColumn @bind-Field="@context.Seq" Text="@WtmBlazor.Localizer["Page.序号"]" />
                <TableColumn @bind-Field="@context.ReceivableDetailCode" Text="@WtmBlazor.Localizer["Page.收款明细单号"]" Visible="false" />
                <TableColumn @bind-Field="@context.SaleId" Text="@WtmBlazor.Localizer["Page.销售订单"]" />
                <TableColumn @bind-Field="@context.Amount4" Text="@WtmBlazor.Localizer["Page.本次结算金额"]" FormatString="0.##" />
                <TableColumn @bind-Field="@context.Amount1" Text="@WtmBlazor.Localizer["Page.单据金额"]" FormatString="0.##" />
                <TableColumn @bind-Field="@context.Amount2" Text="@WtmBlazor.Localizer["Page.已结金额"]" FormatString="0.##" />
                <TableColumn @bind-Field="@context.Amount3" Text="@WtmBlazor.Localizer["Page.未结金额"]" FormatString="0.##" />
                <TableColumn @bind-Field="@context.Remark" Text="@WtmBlazor.Localizer["_Admin.Remark"]" />
                <TableColumn @bind-Field="@context.CreateTime" Text="@WtmBlazor.Localizer["_Admin.CreateTime"]" FormatString="yyyy-MM-dd HH:mm:ss" />
                <TableColumn @bind-Field="@context.UpdateTime" Text="@WtmBlazor.Localizer["_Admin.UpdateTime"]" FormatString="yyyy-MM-dd HH:mm:ss" />
                <TableColumn @bind-Field="@context.CreateBy" Text="@WtmBlazor.Localizer["_Admin.CreateBy"]" />
                <TableColumn @bind-Field="@context.UpdateBy" Text="@WtmBlazor.Localizer["_Admin.UpdateBy"]" />
            </TableColumns>

            <FooterTemplate>
                <tr style="background-color:gray; font-weight:600; text-align:center;">
                    <td colspan="2">合计：</td>
                    <td style="color:red;">@sumAmount4</td>
                    <td>@sumAmount1</td>
                    <td> @sumAmount2</td>
                    <td> @sumAmount3</td>
                </tr>
            </FooterTemplate>
        </Table>

        <Row>
            <Textarea @bind-Value="@Model.Remark" ShowLabel="true"></Textarea>
        </Row>
        <div class="modal-footer table-modal-footer">
            <Button ButtonType="ButtonType.Submit" IsAsync="true" OnClick="OnAgree" Text="@WtmBlazor.Localizer["Page.同意"]" />
            <Button ButtonType="ButtonType.Submit" IsAsync="true" OnClick="OnDeny" Color="Color.Danger" Text="@WtmBlazor.Localizer["Page.拒绝"]" />
            <Button OnClick="OnClose" Text="@WtmBlazor.Localizer["Sys.Close"]" />
        </div>

    </ValidateForm>

</div>



@code {
    [Parameter]
    public string id { get; set; }
    [Parameter]
    public string[] ids { get; set; }
    private ReceivableVM Model = new ReceivableVM();
    private ValidateForm vform { get; set; }
    private string sumAmount1 { get; set; }
    private string sumAmount2 { get; set; }
    private string sumAmount3 { get; set; }
    private string sumAmount4 { get; set; }
    public void OnClose()
    {
        CloseDialog();
    }
    private async Task Submit(EditContext context)
    {

        Model.Entity.ReceivableDetail_Receivable = ReceivableDetailReceivableList2.ToList();

        await PostsForm(vform, "/api/Fin/Receivable/Approve", (s) => "Sys.OprationSuccess");
    }
    public void OnAgree()
    {
        Model.ActionName = "同意";
        Model.Entity.Status = nERP.Model.StatusTypeEnum.Reviewed;
        Model.Entity.ReceivableDetail_Receivable.ForEach(z0 => z0.Status = nERP.Model.dStatusTypeEnum.Reviewed);
    }
    public void OnDeny()
    {
        Model.ActionName = "拒绝";
    }


    private List<SelectedItem> AllCustomers = new List<SelectedItem>();
    private List<SelectedItem> AllMakeUsers = new List<SelectedItem>();
    private List<SelectedItem> AllSettlementAccounts = new List<SelectedItem>();
    private List<SelectedItem> AllSettlementTypes = new List<SelectedItem>();
    private IEnumerable<ReceivableDetail> ReceivableDetailReceivableList2 { get; set; } = new List<ReceivableDetail>();
    protected override async Task OnInitializedAsync()
    {


        AllCustomers = await WtmBlazor.Api.CallItemsApi("/api/Fin/Receivable/GetCustomers");
        AllCustomers.Insert(0, new SelectedItem { Text = WtmBlazor.Localizer["Sys.PleaseSelect"], Value = "" });
        AllMakeUsers = await WtmBlazor.Api.CallItemsApi("/api/Fin/Receivable/GetFrameworkUsers");
        AllMakeUsers.Insert(0, new SelectedItem { Text = WtmBlazor.Localizer["Sys.PleaseSelect"], Value = "" });
        AllSettlementAccounts = await WtmBlazor.Api.CallItemsApi("/api/Fin/Receivable/GetSettlementAccounts");
        AllSettlementAccounts.Insert(0, new SelectedItem { Text = WtmBlazor.Localizer["Sys.PleaseSelect"], Value = "" });
        AllSettlementTypes = await WtmBlazor.Api.CallItemsApi("/api/Fin/Receivable/GetDicFields结算方式");
        AllSettlementTypes.Insert(0, new SelectedItem { Text = WtmBlazor.Localizer["Sys.PleaseSelect"], Value = "" });
        if (id != "")
        {
            var rv = await WtmBlazor.Api.CallAPI<ReceivableVM>($"/api/Fin/Receivable/{id}");
            Model = rv.Data;
        }
        ReceivableDetailReceivableList2 = Model.Entity.ReceivableDetail_Receivable;
        var Items = Model.Entity.ReceivableDetail_Receivable;

        sumAmount1 = Items.Any() ? Items.Sum(x => x.Amount1)?.ToString("#0.####") : "0";
        sumAmount2 = Items.Any() ? Items.Sum(x => x.Amount2)?.ToString("#0.####") : "0";
        sumAmount3 = Items.Any() ? Items.Sum(x => x.Amount3)?.ToString("#0.####") : "0";
        sumAmount4 = Items.Any() ? Items.Sum(x => x.Amount4)?.ToString("#0.####") : "0";

        await base.OnInitializedAsync();
    }
}
